package com.stnet.web.enterprice.chinamobile;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.modules.commons.PageAction;
import com.modules.util.Mycomparator;
import com.stnet.entity.Roles;
import com.stnet.entity.RolesAuthorities;
import com.stnet.entity.RolesAuthoritiesId;

public class AgentpayActionAction extends PageAction {

	private Roles roles;
	private List<Roles> rolesList;
	private List authoritiesList;
	private Long id;
	private String name;
	private List checkedMenu;

	public AgentpayActionAction() {
		roles = new Roles();
	}

	public List getCheckedMenu() {
		return checkedMenu;
	}

	public void setCheckedMenu(List checkedMenu) {
		this.checkedMenu = checkedMenu;
	}

	public String execute() throws Exception {
		super.pager = rolesManagerDao.findRolesByType(pageSize, pageNo, "-1",
				"2");
		rolesList = super.pager.getResultList();
	
		for(Roles role : rolesList){
			
		    List<RolesAuthorities> list = null;
		    list = new ArrayList<RolesAuthorities>();//role.getRaList();
		    
		    for(RolesAuthorities ra : role.getRolesAuthoritieses()){
		    	list.add(ra);
		    }
		    Comparator comp = new Mycomparator();
		    Collections.sort(list,comp); 
		    role.setRaList(list);
		}
		
		return "success";
	}

	public String view() throws Exception {
		roles = rolesManagerDao.getRoles(id);
		return "view";
	}

	public String edit() throws Exception {
		setAuthoritiesList(authoritiesManagerDao.findAuthorities("2","0"));
		setRoles(rolesManagerDao.getRoles(id));
		return "edit";
	}

	public String checkName() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("name");
		String orgName = request.getParameter("orgName");
		name = URLDecoder.decode(name, "UTF-8");
		orgName = URLDecoder.decode(orgName, "UTF-8");
		if (rolesManagerDao.isNameUnique(name, orgName))
			return renderText("true");
		else
			return renderText("false");
	}

	public String update() throws Exception {
		roles.setType("2");
		rolesManagerDao.updateRoles(roles);
		rolesAuthoritiesManagerDao.deleteRolesAuthoritiesByRoles(roles.getId());
		com.stnet.entity.Authorities authorities;
		HttpServletRequest request = ServletActionContext.getRequest();
		String value = null;
		String reValue = null;
		
		for (Iterator iterator = checkedMenu.iterator(); iterator.hasNext(); rolesAuthoritiesManagerDao
				.addRolesAuthorities(new RolesAuthorities(
						new RolesAuthoritiesId(roles, authorities), roles,
						authorities, value, reValue))) {
			String check = (String) iterator.next();
			authorities = authoritiesManagerDao.getAuthorities(Long
					.valueOf(check));

			value = null;
			reValue = null;
			value = (String) request.getParameter("ep_" + check);
			reValue = (String) request.getParameter("epre_" + check);
		}

		return "update";
	}

	public String add_page() throws Exception {

		setAuthoritiesList(authoritiesManagerDao.findAuthorities("2","2"));
		return "add_page";
	}

	public String add() throws Exception {
		initRoles();
		roles.setType("2");
		rolesManagerDao.addRoles(roles);
		com.stnet.entity.Authorities authorities;
		
		HttpServletRequest request = ServletActionContext.getRequest();
        
		String value = null;
		String reValue = null;
		for (Iterator iterator = checkedMenu.iterator(); iterator.hasNext(); rolesAuthoritiesManagerDao
				.addRolesAuthorities(new RolesAuthorities(
						new RolesAuthoritiesId(roles, authorities), roles,
						authorities, value, reValue))) {

			String check = (String) iterator.next();
			authorities = authoritiesManagerDao.getAuthorities(Long
					.valueOf(check));

			value = null;
			reValue = null;
			value = (String) request.getParameter("ep_" + check);
			reValue = (String) request.getParameter("epre_" + check);
			
		}

		return "add";
	}

	public String delete() throws Exception {
		rolesManagerDao.deleteRoles(id);
		return "delete";
	}

	public void initRoles() {
		roles.setName(name);
	}

	public Roles getRoles() {
		return roles;
	}

	public void setRoles(Roles roles) {
		this.roles = roles;
	}

	public List getRolesList() {
		return rolesList;
	}

	public void setRolesList(List rolesList) {
		this.rolesList = rolesList;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List getAuthoritiesList() {
		return authoritiesList;
	}

	public void setAuthoritiesList(List authoritiesList) {
		this.authoritiesList = authoritiesList;
	}

}